package com.lanmaly.cunzheng.decorator;

import java.io.*;

/**
 * <p>装饰者设计模式</p>
 * <p>非常符合开闭原则，对拓展开放，对修改关闭</p>
 * 动态地给一个对象增加一些额外的职责，增加对象功能来说，装饰模式比生成子类实现更为灵活。装饰模式是一种对象结构型模式。
 * 若要拓展功能，装饰者设计模式比继承更有弹性
 *
 * @author hou 15373178086@163.com
 */
public class LowerCaseInputStream extends FilterInputStream {

    /**
     * Creates a <code>FilterInputStream</code>
     * by assigning the  argument <code>in</code>
     * to the field <code>this.in</code> so as
     * to remember it for later use.
     *
     * @param in the underlying input stream, or <code>null</code> if
     *           this instance is to be created without an underlying stream.
     */
    protected LowerCaseInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        int c = super.read();
        return (c == -1 ? c : Character.toLowerCase(c));
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int read = super.read(b, off, len);
        for (int i = off; i < off + read; i++) {
            b[i] = (byte) Character.toLowerCase((char) b[i]);
        }
        return read;
    }

    public static void main(String[] args) throws IOException {
        InputStream is = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("F:/xuec/test.txt")));
        int c;
        while ((c = is.read()) > 0) {
            System.out.print((char) c);
        }
        is.close();
    }
}
